package com.LeeCode;

import java.util.HashMap;

/**
 * 子串的最大出现次数
 */

public class Code1297 {
    public static void main(String[] args) {
        String s = "aababcaab";
        Code1297 c = new Code1297();
        System.out.println(c.maxFreq(s, 2, 3, 4));
    }

    public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {
        HashMap<Character, Integer> charMap = new HashMap<>();
        HashMap<String, Integer> stringMap = new HashMap<>();
        int maxNum = 0;

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            charMap.put(c, charMap.getOrDefault(c, 0) + 1);

            if (i < minSize - 1) continue;

            if (charMap.size() <= maxLetters) {
                String str = s.substring(i - minSize + 1, i + 1);
                stringMap.put(str, stringMap.getOrDefault(str, 0) + 1);
                maxNum = Math.max(maxNum, stringMap.get(str));
//                System.out.println(str);
            }

            c = s.charAt(i - minSize + 1);

            charMap.put(c, charMap.get(c) - 1);
            if (charMap.get(c) == 0) {
                charMap.remove(c);
            }
        }
        return maxNum;
    }
}
